﻿using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Furion.FriendlyException;
using Microsoft.AspNetCore.Authorization;
using System.Collections.Generic;
using System.Linq;

namespace Admin.NET.Application;
/// <summary>
/// 车型服务
/// </summary>
[AllowAnonymous]
[ApiDescriptionSettings(ApplicationConst.GroupNameApi, Order = 100)]
public class car_carServiceApi : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<car_car> _rep;
    public car_carServiceApi(SqlSugarRepository<car_car> rep)
    {
        _rep = rep;
    }

    /// <summary>
    /// 分页查询车型
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<car_tag>> Page(car_carInput input)
    {
        var query = _rep.Context.Queryable<car_tag>()
       .Where(m => m.IsDelete == false && m.type == (int?)car_tagType.car)
                   .WhereIF(!string.IsNullOrWhiteSpace(input.carname), u => u.title.Contains(input.carname.Trim()))
                     .WhereIF(input.pid>-1, u => u.pid== input.pid)
                 
;
        query = query.OrderBuilder(input);
        return await query.ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    ///  查询车型列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Page")]
    public  List<List<car_tag>> Page()
    {
        var query = _rep.Context.Queryable<car_tag>()
       .Where(m => m.IsDelete == false && m.type == (int?)car_tagType.car)
;
        List <List<car_tag>> relist=new List<List<car_tag>> ();
        List<car_tag> list0 = new List<car_tag>();
        list0.Add(new car_tag() {
            title = "所有车型"
        });
        list0.AddRange(query.Where(m => m.pid == 0).ToList());
      


        List<car_tag> listFirst = new List<car_tag>();
        listFirst.Add(new car_tag()
        {
            title = "所有车型"
        });
       // listFirst.AddRange( query.Where(m => m.pid == list0.First().Id).ToList());

        relist.Add(list0);
        relist.Add(listFirst);
        return  relist;
    }


    /// <summary>
    /// 增加车型
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task Add(Addcar_carInput input)
    {
        var entity = input.Adapt<car_car>();
        await _rep.InsertAsync(entity);
    }

    /// <summary>
    /// 删除车型
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(Deletecar_carInput input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _rep.FakeDeleteAsync(entity);   //假删除
    }

    /// <summary>
    /// 更新车型
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(Updatecar_carInput input)
    {
        var entity = input.Adapt<car_car>();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取车型
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<car_car> Get([FromQuery] QueryByIdcar_carInput input)
    {
        return await _rep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取车型列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<car_carOutput>> List([FromQuery] car_carInput input)
    {
        return await _rep.AsQueryable().Select<car_carOutput>().ToListAsync();
    }





}

